package edu.ccut.saturn.authority.core.dept;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnData;

public class GetDeptMapforSelectTag extends DefaultComponent {
	@SuppressWarnings("unchecked")
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {
		List<SaturnData> deptList = (List<SaturnData>) parameterContext
				.getParameterValue(0);
		List<Map<String, String>> deptMapList = this
				.generateDeptSelect(deptList);
		parameterContext.setParameter(1, deptMapList);
	}

	/**
	 * 生成部门Select
	 * 
	 * @param deptList
	 *            部门List
	 * @return deptMapList
	 */
	private List<Map<String, String>> generateDeptSelect(
			List<SaturnData> deptList) {

		List<Map<String, String>> deptMapList = new ArrayList<Map<String, String>>();
		String defaultValue = "--\u8BF7\u9009\u62E9--";
		Map<String, String> defaultDeptMapNode = new HashMap<String, String>();
		defaultDeptMapNode.put("key", "");
		defaultDeptMapNode.put("value", defaultValue);
		deptMapList.add(defaultDeptMapNode);
		if (deptList != null) {
			for (SaturnData dept : deptList) {
				String id = dept.getString("id");
				String value = dept.getString("name");
				Map<String, String> deptMapNode = new HashMap<String, String>();
				deptMapNode.put("key", id);
				deptMapNode.put("value", value);
				deptMapList.add(deptMapNode);
			}
		}
		return deptMapList;
	}
}